package com.xzt.meeting.service;

import com.xzt.meeting.dao.UserDao;
import com.xzt.meeting.entity.PageEntity;
import com.xzt.meeting.entity.User;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/5 22:49
 * @Version 1.0
 * @Introduce
 */
public class UserService {

    private UserDao userDao = new UserDao();

    public User getUserByname(String username) {
        return userDao.findUserByname(username);
    }

    public User getUserByemail(String email) {
        return userDao.findUserByeamil(email);
    }

    public void addUser(User user) {
        //默认设置为私密
        user.setIsSecret("0");
        // 如果不是微信登录用户，真实姓名默认用户名
        if (user.getRealName() == null){
            user.setRealName(user.getUsername());
        }
        // 注册时间为当前
        user.setRegisterTime(new Date());
        //如果不是微信登录用户没有头像，使用默认头像
        if (user.getPic() == null){
            user.setPic("http://192.168.31.21:8080/upload/def.png");
        }
        userDao.save(user);
    }

    public List<User> findAll() {
        return userDao.findAll();
    }

    public List<User> search(String realName) {

        return userDao.search(realName);
    }

    public PageEntity<User> findPage(String realName, Integer currPage) {
        // 声明一个分页实体类
        PageEntity<User> pageEntity = new PageEntity<>();

        // 查询总记录数
        Integer totalCount = userDao.countByRealName(realName);

        // 页大小
        Integer pageSize = pageEntity.getPageSize();

        // 起始索引
        Integer startIndex = (currPage - 1) * pageSize;

        // 查询当前页数据
        List<User> userList = userDao.findPage(realName, startIndex, pageSize);

        // 总页数
    /*
        11---5---3
        15---5---3
        16---5---4
        21---5---5
     */
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;

        pageEntity.setData(userList);
        pageEntity.setPageSize(pageSize);
        pageEntity.setCurrPage(currPage);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setTotalCount(totalCount);

        return pageEntity;
    }

    public List<Map<String, Object>> findUserByDeptId(Long deptId) {
        return userDao.findUserByDeptId(deptId);
    }

    public User findUserById(Long id) {
        return userDao.findUserById(id);
    }

    public void incLook(Long id) {
        userDao.incLook(id);
    }

    public Integer countFansByUserId(Long id) {
        return userDao.countFansByUserId(id);
    }

    public Integer countFocusByUserId(Long id) {
        return userDao.countFocusByUserId(id);
    }

    public void updateUrl(Long id, String picUrl) {
        userDao.updateUrl(id,picUrl);
    }

    public void update(User user) {
        userDao.update(user);
    }

    public List<Long> findFocusListByUserId(Long id) {
        return userDao.findFocusListByUserId(id);
    }

    public Integer focus(Long userid, Long focusid) {
        // 首先判断该用户是否已经添加关注
        List<Long> focusList = userDao.findFocusListByUserId(userid);

        if(focusList !=null && focusList.contains(focusid)){

            // 说明已经加过关注(取消关注)
            userDao.unFocus(userid,focusid);
            return 0;
        }

        // 说明没有加过关注(加关注)
        userDao.focus(userid,focusid);

        return 1;
    }

    public PageEntity<Map<String, Object>> findFocusPage(Long id, Integer currPage) {

        PageEntity<Map<String,Object>> pageEntity = new PageEntity<>();
        pageEntity.setCurrPage(currPage);
        Integer pageSize = pageEntity.getPageSize();
        Integer startIndex = (currPage-1) * pageSize;
        List<Map<String,Object>> data = userDao.findFocusPage(id,startIndex,pageSize);
        Integer totalCount = userDao.countFocusByUserId(id);
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount/pageSize) + 1;
        pageEntity.setData(data);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setTotalPage(totalPage);
        return pageEntity;
    }

    public void updateLoginTime(Long uid) {
        userDao.updateLoginTime(uid);
    }

    public void updatePwdByEmail(String email, String newpwd) {
        userDao.updatePwdByEmail(email,newpwd);
    }

    public User findByWxOpenId(String openid) {
        return userDao.findByWxOpenId(openid);
    }
}
